<template>
  <!-- 二次确认 -->
  <a-modal title="提示" v-model:visible="confirmVisible">
    <a-spin :loading="loading" tip="加载中...">
      <div v-for="(it, i) in resultMsg" :key="i">{{ it }}</div>
      <div>{{ confirmTitle }}</div>
    </a-spin>
    <template #footer>
      <a-button @click="close">关闭</a-button>
      <a-button type="primary" :loading="okLoading" @click="handleConfirm">确认</a-button>
    </template>
  </a-modal>
</template>

<script setup>
  import { ref } from 'vue'
  import { platoonCarCheck } from '@/api/asset/carStock'

  // 排车校验
  const loading = ref(false)
  const resultMsg = ref([])
  const confirmVisible = ref(false)
  const confirmTitle = ref('')
  let cancelCbFn = () => {}
  let confirmCbFn = (cb) => {
    cb()
  }
  let submitBtnLoading_value = ref(false)
  const confirmFn = ({ title = '是否确认提交？', params, onOk, onCancel, submitBtnLoading, required = false }) => {
    if (!params.carId) return
    confirmTitle.value = title
    if (submitBtnLoading) submitBtnLoading_value = submitBtnLoading
    if (onOk) {
      confirmCbFn = onOk
      // 维修保养 开始时间 结束时间 不是必填项
      if (!params.startTime || !params.endTime) {
        handleConfirm()
        return
      }
    }
    if (onCancel) cancelCbFn = onCancel
    if (required) confirmVisible.value = true
    loading.value = true
    platoonCarCheck(params)
      .then((res) => {
        let msgList = res.result.resultMsg || []
        if (msgList.length) {
          resultMsg.value = msgList
          confirmVisible.value = true
        } else {
          if (required == false) handleConfirm()
        }
      })
      .finally(() => {
        loading.value = false
      })
  }

  const okLoading = ref(false)
  const handleConfirm = () => {
    okLoading.value = true
    submitBtnLoading_value.value = true
    confirmCbFn(() => {
      okLoading.value = false
      confirmVisible.value = false
      setTimeout(() => {
        submitBtnLoading_value.value = false
      }, 500)
    })
  }

  const close = () => {
    okLoading.value = false
    confirmVisible.value = false
    submitBtnLoading_value.value = false
    cancelCbFn()
  }

  defineExpose({ confirm: confirmFn })
</script>

<style lang="less" scoped></style>
